from CoTec.utility.file.file_go import FileHelper
from CoTec.core.exception.exception_go import *


class Config():
    path = 'Config/general.config'

    cache_config = None
    cache_dic = {}

    def __init__(self, path:str):
        self.path = path

    @staticmethod
    def loadConfig():
        if Config.cache_config is not None:
            return Config.cache_config
        else:
            Config.cache_config = FileHelper.readLines(Config.path)
            return Config.cache_config

    @staticmethod
    def getPar(key):
        if key in Config.cache_dic.keys():
            return Config.cache_dic[key]

        config = Config.loadConfig()

        for line in config:
            line = "".join(line.split())
            par = line.split('=', 1)

            if len(par) == 2 and par[1] is not None:
                Config.cache_dic[key] = par[1]
            elif par[0] == '':
                continue
            else:
                raise ReadConfigFileException

            if par[0] == key:
                return par[1]

        raise ReadConfigFileException

# config = Config()
# val = config.getPar('boson_key2')
# print('')

